public class Zcsh {
    public int maximalSquare(char[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0;
        char maxValue = '0';
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == '0') continue; 
                        
                if (i != 0 && j != 0) {
                    matrix[i][j] = (char) (Math.min(Math.min((int)matrix[i][j - 1], (int) matrix[i - 1][j]), (int) matrix[i - 1][j - 1]) + 1);
                    maxValue = (char) Math.max((int) matrix[i][j], maxValue);
                }
                maxValue = (char) Math.max((int) matrix[i][j], maxValue);
            }
        }
        int high = (int) maxValue - 48;
        return high * high;
    }
}
